﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sharp.Infrastructure.Security;

namespace Sharp.Domain.CMSBoundedContext.AritcleModule.Aggregates
{
    public partial class GatherUrlCacheRepository
    {
        /// <summary>
        /// 缓存下一页
        /// </summary>
        /// <param name="pageUrl"></param>
        /// <param name="urls"></param>
        /// <remarks>只有大于等于5页才进行缓存</remarks>
        public void CacheNextPages(string pageUrl, string[] urls)
        {
            if (urls.Length < 4)
                return;
            var infos = (from x in urls
                select
                    new GatherUrlCache
                    {
                        PUrl = pageUrl,
                        PUrlMD5 = Hash.Md5(pageUrl),
                        NextPageUrl = x,
                        CacheTime = DateTime.Now
                    }).ToList();

            infos.ForEach(this.Add);
        }

        /// <summary>
        /// 读取缓存的url地址
        /// </summary>
        /// <param name="pageUrl"></param>
        /// <returns></returns>
        public string[] ReadNextPages(string pageUrl)
        {
            var md5 = Hash.Md5(pageUrl);

            return this.Select(x => x.PUrlMD5 == md5, x => x.NextPageUrl).ToArray();
        }
    }
}